<template>
  <div class="son-page-container" :style="{ backgroundImage: `url(${website_bg_url})` }">
    <el-container class="main-container">
      <el-header height="10vh" class="header-container">
        <MyHeader />
      </el-header>
      <el-main>
        <el-row :gutter="20">
          <el-col :span="4">
            <div class="area-container menu-container">
              <div class="menu-item" v-for="(item, index) in menuItems" :key="index">
                <div class="parent-menu" v-if="item.status < 2"
                     @click="toggleSubmenu(index)"
                     :class="{ 'disabled': item.status === 1 }">
                  <el-icon class="menu-icon">
                    <component :is="item.icon" />
                  </el-icon>
                  <span class="menu-title">{{ item.title }}</span>
                  <el-icon class="expand-icon" :class="{ 'is-expanded': item.isOpen }">
                    <ArrowDown/>
                  </el-icon>
                </div>
                <div class="child-menu" v-show="item.isOpen && item.status === 0">
                  <div
                    class="child-item"
                    v-for="(subItem, subIndex) in item.children"
                    :key="subIndex"
                    :class="{
                      'selected': subItem.id === activeId,
                      'disabled': subItem.status === 1
                    }"
                    @click="handleMenuClick(subItem)"
                  >
                    {{ subItem.title }}
                  </div>
                </div>
              </div>
            </div>
          </el-col>
          <el-col :span="20">
            <div class="area-container right-area">
              <component :is="currentComponent" v-if="currentComponent" :key="componentKey" @view-scan-data="handleScanDataView" :selected-activity-id="selectedActivityId" />
            </div>
          </el-col>
        </el-row>
      </el-main>
    </el-container>
  </div>
</template>

<script setup lang="ts">
import {onMounted, ref, computed} from 'vue'
import axiosUtil from '../../axios-util/axios-util'
import { ArrowDown, VideoCamera, User, Connection, DataLine } from '@element-plus/icons-vue'
import DataOverview from "../matrix/DataOverview.vue";
import DouyinPublish from "../matrix/DouyinPublish.vue";
import GroupPublish from "../matrix/GroupPublish.vue";
import GroupPublishData from "../matrix/GroupPublishData.vue";
import RedBookPublish from "../matrix/RedBookPublish.vue";
import ScanPublishData from "../matrix/ScanPublishData.vue";
import MyHeader from '../common/MyHeader.vue';
import utils from '../../utils/utils';

// 网站背景图
const website_bg_url = ref('')
const activeId = ref<number | null>(null)
const currentComponent = ref<any>(null)
const componentKey = ref(0)

const theme = ref(utils.getTheme());

// 本页菜单
const menuItems = ref([
  {
    id:1,
    title: '扫码发布',
    icon: VideoCamera,
    isOpen: true,
    status: 0,
    children: [
      { title: '抖音发布' ,id:11,pId:1,status: 0,},
      // { title: '小红书发布',id:12,pId:1,status: 0 }
    ]
  },
  {
    id:2,
    title: '团队发布',
    icon: User,
    isOpen: false,
    status: 1,
    children: [
      { title: '团队发布',id:21,pId:2,status: 1 }
    ]
  },
  {
    id:3,
    title: '矩阵发布',
    icon: Connection,
    isOpen: false,
    status: 2,
    children: [
      { title: '矩阵发布',id:31,pId:3,status: 0 }
    ]
  },
  {
    id:4,
    title: '数据看板',
    icon: DataLine,
    isOpen: false,
    status: 0,
    children: [
      { title: '扫码发布数据',id:41,pId:4,status: 0 },
      { title: '团队发布数据',id:42,pId:4,status: 1 },
      { title: '数据总览',id:43,pId:4,status: 1 }
    ]
  }
])

const toggleSubmenu = (index: number) => {
  if (menuItems.value[index].status === 0) {
    menuItems.value.forEach((item, i) => {
      item.isOpen = i === index;
    });
  }
}

const handleMenuClick = (item: any) => {
  if (item.status === 0) {
    activeId.value = item.id
    componentKey.value++ // 强制组件重新加载

    switch(item.id) {
      case 11:
        currentComponent.value = DouyinPublish
        break
      case 12:
        currentComponent.value = RedBookPublish
        break
      case 21:
        currentComponent.value = GroupPublish
        break
      // case 31:
      //   window.open('https://zhjh168.com//storeplugin/index?jump_url=tuiguang_user&menu_url=tuiguang_user', '_blank')
      //   break
      case 41:
        currentComponent.value = ScanPublishData
        break
      case 42:
        currentComponent.value = GroupPublishData
        break
      case 43:
        currentComponent.value = DataOverview
        break
      default:
        currentComponent.value = null
    }
  }
}

// 监听DouyinPublish的view-scan-data事件，切换到扫码发布数据，并传递title
const selectedActivityId = ref(null);
const handleScanDataView = (id) => {
  selectedActivityId.value = id;
  // 找到数据看板菜单
  const menu = menuItems.value.find(m => m.id === 4); // 数据看板
  if (menu) {
    menu.isOpen = true;
    const sub = menu.children.find(c => c.id === 41);
    if (sub) handleMenuClick(sub);
  }
}

onMounted(() => {
  initWebsiteBgUrl()
  // 默认选中第一个菜单（id为11）
  handleMenuClick(menuItems.value[0].children[0])
})

//读取配置，获取网站背景图
const initWebsiteBgUrl = async () => {
  const response = await axiosUtil.post('api/pc/site/get_agent_config', {})
  if (response.data.code === 0 && response.data.data) {
    website_bg_url.value = import.meta.env.VITE_APP_SERVER + response.data.data.website_bg || ''
  }
}
</script>

<style scoped>
.son-page-container {
  position: relative;
  width: 100%;
  min-height: 100vh;
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
}

.main-container {
  height: 100vh;
  width: 100%;
  backdrop-filter: blur(5px);
}

.header-container {
  padding: 0;
  width: 100%;
  background: rgba(255, 255, 255, 0.1);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid rgba(255, 255, 255, 0.2);
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}

.el-main {
  padding: 20px;
  height: 90vh;
  overflow-x: hidden;
}

.el-row {
  height: 100%;
  margin: 0 !important;
}

.el-col {
  height: 100%;
}

.area-container {
  height: 100%;
  background: rgba(255, 255, 255, 0.1);
  backdrop-filter: blur(10px);
  border-radius: 12px;
  border: 1px solid rgba(255, 255, 255, 0.2);
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
  transition: all 0.3s ease;
  padding: 15px;
}

.area-container:hover {
  background: rgba(255, 255, 255, 0.15);
  box-shadow: 0 6px 12px rgba(0, 0, 0, 0.15);
  border-color: rgba(255, 255, 255, 0.3);
}

.menu-container {
  overflow-y: auto;
}

.menu-container::-webkit-scrollbar {
  width: 6px;
}

.menu-container::-webkit-scrollbar-thumb {
  background: rgba(255, 255, 255, 0.3);
  border-radius: 3px;
}

.menu-container::-webkit-scrollbar-track {
  background: rgba(255, 255, 255, 0.1);
}

.menu-item {
  margin-bottom: 15px;
}

.parent-menu {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 15px;
  background: rgba(255, 255, 255, 0.1);
  backdrop-filter: blur(10px);
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.3s ease;
  border: 1px solid rgba(255, 255, 255, 0.2);
  color: #fff;
  font-weight: 500;
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}

.parent-menu:hover:not(.disabled) {
  background: rgba(255, 255, 255, 0.2);
  transform: translateY(-2px);
  box-shadow: 0 6px 12px rgba(0, 0, 0, 0.15);
}

.parent-menu.disabled {
  opacity: 0.5;
  cursor: not-allowed;
  background: rgba(128, 128, 128, 0.2);
}

.menu-title {
  flex: 1;
  color: v-bind('theme === "light" ? "rgb(0, 0, 0, 0.8)" : "rgb(255, 255, 255, 0.8)"');
  font-size: 0.95em;
  letter-spacing: 1px;
}

.expand-icon {
  transition: transform 0.3s ease;
  color: #fff;
}

.expand-icon.is-expanded {
  transform: rotate(180deg);
}

.child-menu {
  margin-top: 8px;
  padding: 5px;
  animation: slideDown 0.3s ease;
}

@keyframes slideDown {
  from {
    opacity: 0;
    transform: translateY(-10px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.child-item {
  padding: 10px 15px;
  margin: 8px 0;
  background: rgba(255, 255, 255, 0.1);
  backdrop-filter: blur(10px);
  border-radius: 6px;
  cursor: pointer;
  transition: all 0.3s ease;
  border: 1px solid rgba(255, 255, 255, 0.2);
  color: v-bind('theme === "light" ? "rgb(0, 0, 0, 0.8)" : "rgb(255, 255, 255, 0.8)"');
  font-size: 0.95em;
  text-align: center;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.child-item:hover:not(.disabled) {
  background: rgba(255, 255, 255, 0.2);
  transform: translateY(-2px);
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.15);
  border-color: rgba(255, 255, 255, 0.4);
}

.child-item:active:not(.disabled) {
  transform: translateY(0);
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.child-item.selected {
  background: rgba(64, 158, 255, 0.3);
  border-color: rgba(64, 158, 255, 0.5);
  color: v-bind('theme === "light" ? "rgb(0, 0, 0, 0.8)" : "rgb(255, 255, 255, 0.8)"');
  box-shadow: 0 4px 8px rgba(64, 158, 255, 0.2);
}

.child-item.disabled {
  opacity: 0.5;
  cursor: not-allowed;
  background: rgba(128, 128, 128, 0.2);
}

.right-area {
  min-height: 70vh;
}

.menu-icon {
  margin-right: 10px;
  font-size: 1.1em;
  color: #409EFF;
}
</style>
